package br.com.q7solucoes.lazerbrwebservicejclient;

import br.com.q7solucoes.lazerbrwebservicejclient.ws.LazerBRWebServiceJ;
import br.com.q7solucoes.lazerbrwebservicejclient.ws.LazerBRWebServiceJ_Service;
import br.com.q7solucoes.lazerbrwebservicejclient.ws.WpPost;
import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.BindingProvider;

/**
 * Cliente do LazerBRWebServiceJ.
 *
 * @author Elenilson Vieira - elenilson[at]q7solucoes.com.br
 * @since 27 de fevereiro de 2012
 * @version 1.0
 * @see www.q7solucoes.com.br
 */
public class LazerBRWebServiceJClient implements LazerBRWebServiceJ{
        
    private LazerBRWebServiceJ lazerBRWebServiceJClient;
    
    /**
     * Construtor
     */
    public LazerBRWebServiceJClient() {
        lazerBRWebServiceJClient = new LazerBRWebServiceJ_Service().getLazerBRWebServiceJPort();
        
        ((BindingProvider) lazerBRWebServiceJClient).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
    }

    /**
     * Adiciona um post
     * 
     * @param post o post a ser adicionado
     * 
     * @return verdadeiro se foi possivel adicionar e falso caso contrario
     */
    @Override
    public boolean addPost(WpPost post) {
        return lazerBRWebServiceJClient.addPost(post);
    }

    /**
     * Adiciona um post
     * 
     * @param title o titulo
     * @param content o conteudo
     * @param link o link
     * @param imageLink o link da imagem
     * @param date a data
     * @param categories as categorias
     * @param tags as tags
     * 
     * @return verdadeiro se foi possivel adicionar e falso caso contrario
     */
    public boolean addPost(String title, String content, String link, String imageLink, GregorianCalendar date, List<String> categories, List<String> tags) {
        XMLGregorianCalendar dateCalendar = new XMLGregorianCalendarImpl(date);
        
        WpPost post = new WpPost();
        post.setTitle(title);
        post.setContent(content);
        post.setImage(imageLink);
        post.setDate(dateCalendar);
        post.getCategories().addAll(categories);
        post.getTags().addAll(tags);
        
        return addPost(post);
    }

    /**
     * Retorna a lista de posts de um intervalo
     * 
     * @param beginDate a data inicial do intervalo
     * @param endDate a data final do intervalo
     * 
     * @return a lista de posts
     */
    @Override
    public List<WpPost> getPosts(XMLGregorianCalendar beginDate, XMLGregorianCalendar endDate) {
        return lazerBRWebServiceJClient.getPosts(beginDate, endDate);
    }

    /**
     * Retorna a lista de posts de um intervalo
     * 
     * @param beginDate a data inicial do intervalo
     * @param endDate a data final do intervalo
     * 
     * @return a lista de posts
     */
    public List<WpPost> getPosts(Date beginDate, Date endDate) {
        GregorianCalendar beginCalendar = new GregorianCalendar();
        beginCalendar.setTime(beginDate);
        GregorianCalendar endCalendar = new GregorianCalendar();
        beginCalendar.setTime(endDate);
        
        XMLGregorianCalendar beginDateXMLCalendar = new XMLGregorianCalendarImpl(beginCalendar);
        XMLGregorianCalendar endDateXMLCalendar = new XMLGregorianCalendarImpl(endCalendar);
        
        return lazerBRWebServiceJClient.getPosts(beginDateXMLCalendar, endDateXMLCalendar);
    }
}
